
#include "server.h"

namespace secsmart_tcpip
{
int ServerManager::add_server(IPAddress &ip, uint16_t port, uint8_t protocol, uint8_t status)
{
    server_key_t key(ip, port, protocol);

    pthread_rwlock_wrlock(&rwlock_);
    auto it = server_map_.find(key);
    if (it != server_map_.end()) { // server 已经存在
        pthread_rwlock_unlock(&rwlock_);
        return SERVER_ERROR;
    }

    shared_ptr<Server> server(new Server);
    if (server == nullptr) {
        pthread_rwlock_unlock(&rwlock_);
        return SERVER_ERROR;
    }

    server->ip = ip;
    server->port = port;
    server->protocol = protocol;
    server->status = status;
    server_map_.insert(make_pair(key, server));
    pthread_rwlock_unlock(&rwlock_);
    return SERVER_OK;
}

int ServerManager::del_server(IPAddress &ip, uint16_t port, uint8_t protocol)
{
    server_key_t key(ip, port, protocol);

    pthread_rwlock_wrlock(&rwlock_);
    auto it = server_map_.find(key);
    if (it == server_map_.end()) { // server 不存在
        pthread_rwlock_unlock(&rwlock_);
        return SERVER_ERROR;
    }

    // 从map中删除
    server_map_.erase(it);
    pthread_rwlock_unlock(&rwlock_);
    return SERVER_OK;
}

bool ServerManager::find_server(IPAddress &ip, uint16_t port, uint8_t protocol)
{
    bool result;
    server_key_t key(ip, port, protocol);

    pthread_rwlock_rdlock(&rwlock_);
    auto it = server_map_.find(key);
    result = (it != server_map_.end());
    pthread_rwlock_unlock(&rwlock_);
    return result;
}

shared_ptr<Server> ServerManager::get_server(IPAddress &ip, uint16_t port, uint8_t protocol)
{
    shared_ptr<Server> server;
    server_key_t key(ip, port, protocol);
    pthread_rwlock_rdlock(&rwlock_);
    auto it = server_map_.find(key);
    if (it != server_map_.end()) { // server 不存在
        server = it->second;
    }
    pthread_rwlock_unlock(&rwlock_);

    return server;
}

bool ServerManager::is_work_server(IPAddress &ip, uint16_t port, uint8_t protocol)
{
    bool result;
    server_key_t key(ip, port, protocol);

    pthread_rwlock_rdlock(&rwlock_);
    auto it = server_map_.find(key);
    if (it == server_map_.end()) { // server 不存在
        result = false;
    } else {
        result = (it->second->status == SERVER_ENABLE);
    }
    pthread_rwlock_unlock(&rwlock_);

    return result;
}

int ServerManager::modify_server_status(IPAddress &ip, uint16_t port, uint8_t protocol, uint8_t status)
{
    server_key_t key(ip, port, protocol);

    pthread_rwlock_wrlock(&rwlock_);
    auto it = server_map_.find(key);
    if (it == server_map_.end()) { // server 不存在
        pthread_rwlock_unlock(&rwlock_);
        return SERVER_ERROR;
    }
    it->second->status = status;
    pthread_rwlock_unlock(&rwlock_);
    return SERVER_OK;
}

int ServerManager::get_server_num()
{
    return server_map_.size();
}

void ServerManager::clear_server()
{
    server_map_.clear();
}

} // namespace secsmart_tcpip